<template>
  <div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form size="small" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="名称搜索">
           <el-input v-model="formInline.name" placeholder="请输入名称搜索"></el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select size="small" v-model="formInline.category_id" placeholder="所有分类">
            <el-option label="全部" value="0"></el-option>
            <el-option v-for="(item, index) in categoryList" :key="index" :label="item.name"
                       :value="item.category_id"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="是否显示" prop="express_id">
          <el-select size="small" v-model="formInline.is_show" placeholder="请选择">
            <el-option label="显示" :value="0"></el-option>
            <el-option label="不显示" :value="1"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
          <el-button type="info" icon="el-icon-refresh-right" @click="resetSub">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
          <el-table-column prop="product_id" label="商品ID" width="80"></el-table-column>
          <el-table-column prop="image" label="商品图片">
            <template slot-scope="scope">
              <img :src="scope.row.product.image[0].file_path" width="30px" height="30px"/>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="商品名称">
            <template slot-scope="scope">
              {{ scope.row.product.product_name }}
            </template>
          </el-table-column>
          <el-table-column prop="brand_name" label="品牌"></el-table-column>
          <el-table-column prop="yieldly" label="产地"></el-table-column>
          <el-table-column prop="net_content" label="净含量ml"></el-table-column>
          <el-table-column prop="category_name_text" label="分类"></el-table-column>
          <!-- <el-table-column prop="create_time" label="创建时间" width="140"></el-table-column> -->
          <el-table-column fixed="right" label="操作" width="170">
            <template slot-scope="scope">
              <el-button @click="copyClick(scope.row)" type="text" size="small">一键复制
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
                       :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
                       :total="totalDataNumber"></el-pagination>
      </div>
    </div>
    <div class="common-button-wrapper">
      <el-button size="small" type="info" @click="cancelFunc">返回</el-button>
    </div>
  </div>
</template>

<script>
import PorductApi from '@/api/product.js';

export default {
  components: {},
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      formInline: {
        name:'',
        is_show: 0,
        category_id:'0',
      },
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      userModel: {},
      /*等级*/
      gradeList: [],
      /*全部分类*/
      categoryList: [],
      open_tag: false,
      /*所有标签*/
      allTag: [],
      category: [],
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },

    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = self.formInline;
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      PorductApi.getTemplate(Params, true)
        .then(data => {
          self.loading = false;
          self.tableData = data.data.list.data;
          self.totalDataNumber = data.data.list.total;
          self.category = data.data.category;
          self.categoryList = data.data.category;
        })
        .catch(error => {
          self.loading = false;
        });
    },

    /*搜索查询*/
    onSubmit() {
      let self = this;
      self.loading = true;
      let Params = self.formInline;
      self.curPage = 1;
      self.getTableList();
    },
    // 重置搜索
    resetSub() {
      let self = this;
      self.formInline.name=''
      self.formInline.is_show=0
      self.onSubmit();
    },

    /*打开添加*/
    addClick(item) {
      this.$router.push({
        path: '/setting/dic/edit',
        query: {
          id: 0
        }
      });
    },
    /*打开编辑*/
    editClick(row) {
      this.$router.push({
        path: '/product/product/edit',
        query: {
          product_id: row.product_id,
          scene: 'edit'
        }
      });
    },
    copyClick(row) {
      this.$router.push({
        path: '/product/product/edit',
        query: {
          product_id: row.product_id,
          scene: 'copy'
        }
      });
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == 'add') {
        this.open_add = e.openDialog;
        if (e.type == 'success') {
          this.getTableList();
        }
      }
      if (f == 'edit') {
        this.open_edit = e.openDialog;
        if (e.type == 'success') {
          this.getTableList();
        }
      }
    },

    /*删除用户*/
    deleteClick(row) {
      let self = this;
      self
        .$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          self.loading = true;
          PorductApi.deleteTemplate({
              id: row.id
            },
            true
          )
            .then(data => {
              self.loading = false;
              if (data.code == 1) {
                self.$message({
                  message: '恭喜你，删除成功',
                  type: 'success'
                });
                self.getTableList();
              } else {
                self.loading = false;
              }
            })
            .catch(error => {
              self.loading = false;
            });
        })
        .catch(() => {
          self.loading = false;
        });
    },
    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    },
  }
};
</script>
<style>
.subject-wrap{
  padding: 50px;
}
</style>
